#ifndef _protocol_upmotor_h_
#define _protocol_upmotor_h_

#include "protocol.h"
#include "CRC_Generate.h"
/*
note: this part is reference from modbus
sci communication parameter:
speed:9600
checksum:even
stopbit:1
slave add:0x01
*/
typedef struct upmotor_result{
	uint16_t	pwm;
	uint16_t	current;
	int16_t		freq;
	uint16_t	pos_cont_status;
	uint16_t	pos_high_half;
	uint16_t	pos_low_half;
	uint16_t	res_time_high_half;
	uint16_t	res_time_low_half;
	uint16_t	in1_volt;
	uint16_t	in2_volt;
	uint16_t	in3_volt;
	uint16_t	div_volt;
	uint16_t	sq1_volt;
	uint16_t	sq2_volt;
	uint16_t	in1_input_pwmduty;
	uint16_t	in1_input_freq;
	uint16_t	in1_input_pulsecount_high_half;
	uint16_t	in1_input_pulsecount_low_half;
	uint16_t	halt_status;
	uint16_t	error_status;
	uint16_t	motor_speed;
	uint16_t	motor_speed_ratio;
}up_resultInfo, *up_PresultInfo;
typedef struct{
	uint8_t  timer;
	uint8_t  count;
	uint8_t  len;
	uint8_t  step;
	uint16_t crc16;
	uint16_t crc16_T;
}up_info,*up_Pinfo;

extern up_resultInfo	upmotor_result;

#define 	upmotor_ID			(1)
#define   	upmotor_group		(4)



extern uint8_t upmotor_process(pmesg_prot mesg);
extern uint8_t upmotor_rxFunc(void);
extern uint8_t upmotor_txFunc(void);
extern uint8_t upmotor_period_callback(void);

/*
ask:
01 03 00 20 00 15 85 CF 
resp:
01 03 2A 
00 00 00 00 00 
00 00 01 FF FF 
F8 0D 00 00 00 
00 0C DE 0C DE 
0C E1 00 00 00 
01 00 01 03 E8 
00 00 00 00 00 
00 00 00 00 00 
FF FF 
E7 1C
note: 
    the u8_upmotor_idealtimer should be update by timer
*/
extern void upmotor_rxFunc_callback(uint8_t data_rx);
/*
Cmds
*/

/*
status check:01 03 00 20 00 15 85 CF
*/
extern uint8_t upmotor_status_Get(void);
/*
setting speed and position:
    01 10 00 44 00 04 08 03 E8 00 01 00 00 00 F4 52 D6 
note:
  the speedHz is 0.1hz为精度
  the positionLen has signed  
*/
extern uint8_t upmotor_speedandposition_Set(int16_t speedHz, uint16_t positionMode, int32_t positionLen);
extern uint8_t upmotor_stop(void);
extern void upmotor_init(FunType_bufferTx txFunc);
extern uint8_t upmotor_firstPosition_Set(void);
extern uint8_t Ingrap_Action_down(int32_t upmotor_Loopdegree);
extern uint8_t Ingrap_Action_Up(int32_t upmotor_Loopdegree);
extern uint8_t upmotor_interfaceTest(void);
extern uint8_t upmotor_task(void);
extern void upmotor_lowTxfunc_Register(FunType_bufferTx txFunc);

#endif
